﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Browser;

namespace SkyDriveConnector
{
    public partial class GenericViewModel<T> : DependencyObject
    {

        

        private void connectorOperations_AddSkyDriveItemComplete(object sender, EventArgs e)
        {
            if (((List<SkyDriveResponse>)((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Results)[0].source != null)
            {
                SkyDriveObject skdobj = new SkyDriveObject();
                skdobj.name = HttpUtility.UrlDecode(((List<SkyDriveResponse>)((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Results)[0].filename);
                skdobj.id = ((List<SkyDriveResponse>)((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Results)[0].id;
                skdobj.source = ((List<SkyDriveResponse>)((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Results)[0].source;
                skdobj.parent_id = ((List<SkyDriveResponse>)((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Results)[0].parentid;
                ////_localSpInstance.AddNewItem((SharePointPath, skdobj,);

                Dispatcher.BeginInvoke(() =>
                    {
                        GenericViewModel<FieldValue> viewmodel = new GenericViewModel<FieldValue>();
                        viewmodel.AddNewItem(SharePointPath, skdobj);
                    });
            }

            AddResponseMessage(((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Message);
            connectorOperations.GetItems(ItemPath,App.CorrelationId);
        }

        

        private void connectorOperations_AddSharePointFolderComplete(object sender, EventArgs e)
        {
        }

        private void connectorOperations_AddSharePointItemComplete(object sender, EventArgs e)
        {
            AddResponseMessage(((ItemLoadingCompleteEventArgs<SharePointResponse>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<SharePointResponse>)e).Message);
        }

        private void connectorOperations_AddSkyDriveFolderComplete(object sender, EventArgs e)
        {
        }

        private void connectorOperations_DeleteSharePointItemComplete(object sender, EventArgs e)
        {
            AddResponseMessage(((ItemLoadingCompleteEventArgs<SharePointResponse>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<SharePointResponse>)e).Message);
        }

        private void connectorOperations_DeleteSkyDriveItemComplete(object sender, EventArgs e)
        {
            if (ItemPath == null)
                ItemPath = "";


            AddResponseMessage(((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<SkyDriveResponse>)e).Message);
            connectorOperations.GetItems(ItemPath,App.CorrelationId);

        }

        private void connectorOperations_SharePointItemsLoadingComplete(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
               
                itemsCollection.Clear();

                foreach (T item in ((ItemLoadingCompleteEventArgs<T>)e).Results)
                {
                    itemsCollection.Add(item);
                }

                AddResponseMessage(((ItemLoadingCompleteEventArgs<T>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<T>)e).Message);

                foreach (var item in ((ItemLoadingCompleteEventArgs<FieldValue>)e).Results)
                {
                    if (((FieldValue)item).FieldDisplayName.ToUpper() == "ID")
                    {
                        HtmlPage.Window.Invoke("NavigateDispForm", string.Format("{0}/Lists/SkyDriveList/DispForm.aspx?ID={1}",SPHelpers.GetUrl(), item.Value));
                        
                    }
                }
               
                
            });
            
        }

        

        private void connectorOperations_SkyDriveItemsLoadingComplete(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                itemsCollection.Clear();

                foreach (T item in ((ItemLoadingCompleteEventArgs<T>)e).Results)
                {
                    itemsCollection.Add(item);
                }

                AddResponseMessage(((ItemLoadingCompleteEventArgs<T>)e).CorrelationId, ((ItemLoadingCompleteEventArgs<T>)e).Message);
            });
        }

        private void connectorOperations_SharePointItemLoadingOnError(object sender, EventArgs e)
        {
            
        }

        private void connectorOperations_SkyDriveItemLoadingOnError(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //MessageBox.Show(((ItemLoadingErrorEventArgs)e).ErrorMessage);
                AddResponseMessage(0, ((ItemLoadingErrorEventArgs)e).ErrorMessage);

            });
        }


        private void AddResponseMessage(int correlationId, string message)
        {
            ResponseMessage respmsg = new ResponseMessage
            {
                CorrelationId = correlationId,
                Message = message,
                TimeStamp = DateTime.Now

            };

            if (App.Logger.ResponseMessages == null)
                App.Logger.ResponseMessages = new ObservableCollection<ResponseMessage>();

            Dispatcher.BeginInvoke(() =>
           {
               App.Logger.ResponseMessages.Add(respmsg);
           });
        }


        
    }
}
